package com.koro.controller;


import com.koro.entity.Users;
import com.koro.mapper.UsersMapper;
import com.koro.vo.BaseException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.nio.file.AccessDeniedException;

@Api(tags = "全局异常测试")
@RestController
public class GlobalExceptionController {

    @Autowired
    private UsersMapper usersMapper;

    @GetMapping("/auth")
    @ApiOperation(value = "权限异常")
    public void auth() throws AccessDeniedException {
        throw new AccessDeniedException("暂无权限");
    }

    @PostMapping("/requestMethod")
    @ApiOperation(value = "请求方式")
    public void requestMethod() {
        System.out.println("request");
    }

    @PostMapping("/valid")
    @ApiOperation(value = "参数异常校验")
    public void Valid(@Valid @RequestBody Users users){
        System.out.println(users.toString());
    }

    @GetMapping("/badSqlGrammarException")
    @ApiOperation(value = "数据库异常")
    public void badSqlGrammarException() throws Exception {
        usersMapper.select();
    }

    @GetMapping("/runtimeException")
    @ApiOperation(value = "运行异常")
    public void runtimeException(){
        throw new RuntimeException();
    }

    @GetMapping("/baseException")
    @ApiOperation(value = "自定义业务异常")
    public void baseException() {
        throw new BaseException("业务异常");
    }

    @GetMapping("/exception")
    @ApiOperation(value = "全局异常")
    public void exception() throws Exception {
        throw new Exception("全局异常！");
    }
}
